<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri ="http://java.sun.com/jsp/jstl/functions"%>
<div class="page-header">
	<h3>新增产品</h3>
</div>
<form role="form">
	<div class="form-group">
		<label for="name">产品名称</label><input name="name" type="text"
			class="form-control" id="name" /><br /> <label for="description">产品描述</label>
		<textarea name="description" class="form-control" rows="4"
			style="resize: none;"></textarea>
		<br /> <label>所属部门</label> <select name="department"
			class="form-control">
			<c:forEach var="department" items="${result.object}">
				<option id="${department.id}">${fn:escapeXml(department.name)}</option>
			</c:forEach>
		</select>
	</div>
	<input name="submit" type="submit" value="提交" class="btn btn-default" />
</form>
<script>
	$(function() {
		$("form").submit(
				function(e) {
					var name = $("input[name=name]").val();
					var description = $("textarea[name=description]").val();
					var departmentId = $("select[name=department] :selected")
							.attr("id");
					if (name == null || name == "") {
						layer.tips('名称不能为空', 'input[name=name]', {
							tips : 3
						});
					} else if (description == null || description == "") {
						layer.tips('描述不能为空', 'textarea[name=description]', {
							tips : 3
						});
					} else {
						$.post("${basePath}/product/add", {
							name : name,
							description : description,
							departmentId : departmentId
						}, function(data) {
							if (data.result.success) {
								layer.msg(data.result.message, {
									icon : 1,
									time : 1000
								}, function() {
									$("#rightDiv").load(
											"${basePath}/product/add");
								});
							} else if (data.result.message == "产品名已存在") {
								layer.tips(data.result.message,
										'input[name=name]', {
											tips : 3
										});
							} else {
								layer.msg(data.result.message, {
									icon : 2,
									time : 2000
								});
							}
						});
					}
					return false;
				});
	})
</script>